var cheerio = require("cheerio");
var async = require("async");
var utils = require('../dao/utils');
var Spider = require("./spider");
var DB = require("./spider.db");

var insertDB = require("./insert.db");

var spider = new Spider("http://www.1kkk.com/manhua-jpkr-p*PageReplace*/");
var db = new DB("demo","demo");

spider.start(fun_urls,fun_pages,fun_detail);



function fun_urls(document){
    var $ = cheerio.load(document);

    var liArry = $('.mh-list.col7 li'), urls = [];

    for (var i = 0; i < liArry.length; i++) {
        var item = liArry.eq(i);
        if (item.hasClass('ad_360_item')) {
            continue;
        }
        var url = "http://www.1kkk.com" + item.find('.mh-item-detali .title a').eq(0).attr('href');
        
        urls.push(url);
    }

    return urls;
}


function fun_pages(document){
    var $ = cheerio.load(document);

    return 266;//266
}

function fun_detail(url,document){

    var $ = cheerio.load(document);

    var item = $('#tempc .detail-list-title a');

    if (item.length == 0) {
        return false;
    }

    var chapter = [];

    var chaptersLis = $("#detail-list-select-1 li");


    for (var i = 0; i < chaptersLis.length; i++) {
        var _cur = chaptersLis.eq(i).find('a');

        chapter.push({
            href: "http://www.1kkk.com" + utils.trim(_cur.attr('href')),
            chapter: utils.trim(_cur.text()).replace(/\s+/g,"")
        });
    }
    // var _sChapter = chapter.join(',');
    var _news = $('#tempc .detail-list-title .s a').text().replace(/(^\s*)|(\s*$)/g, "");
 
    var score = utils.trim($('.banner_detail_form .info .title .score').text());

    var _title = $('.banner_detail_form .info .title').text().replace(score, "");
    _title = utils.trim(_title);

    // info
    var info = $('.banner_detail_form .info .content').text().replace(/[\r\n]/g, "");

    if (info.indexOf('[+展开]') != -1) {
        info = info.replace('[+展开]', '');
        info = info.replace('[-折叠]', '');
    }

    var time = $('#tempc .detail-list-title .s span').text();

    var cutWord = $('#tempc .detail-list-title .s span a').text();

    time = time.replace(cutWord,'');

    time = utils.trim(time);

    time = utils.timeFormat(time);

    var data = {
        title: _title,
        href: url,
        img: $('.banner_detail_form .cover img').attr('src'),
        auth: $('.banner_detail_form .info .subtitle a') ? $('.banner_detail_form .info .subtitle a').eq(0).text().replace(/(^s*)|(s*$)/g, "") : null,

        auth_href: "http://www.1kkk.com" + $('.banner_detail_form .info .subtitle a') ? $('.banner_detail_form .info .subtitle a').eq(0).attr('href') : null,

        news: chapter.length>0? chapter[0]['chapter']:null,
        news_href: chapter.length>0?chapter[0]['href']:null,

        isFinish: utils.trim($('.banner_detail_form .info .tip .block').eq(0).find('span').text()) == '已完结' ? true : false,

        updateTime: time,

        type: $('.banner_detail_form .info .tip .block').eq(1).find('span').text(),

        letter: utils.letter(_title),

        info: info,
        domain: "http://www.1kkk.com",
        isUpdate: true,
        "chapters": chapter
    };

    insertDB(data);



}